package com.dubbo.cp.provider.serviceImpl;

import java.util.ArrayList;
import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import com.alibaba.dubbo.config.annotation.Service;
import com.dubbo.cp.IService.BookService;
import com.dubbo.cp.entity.BookEntity;
import com.dubbo.cp.provider.repository.BookRepository;

@Service
public class BookServiceImpl implements BookService{

	@Autowired
	private BookRepository bookRepository;
	
	@Override
	public BookEntity getBookByBookName(String bookName) {
		
		return bookRepository.findByBookName(bookName);
	}
	@Override
	public BookEntity addBookEntity(BookEntity bookEntity) {
		
		return bookRepository.save(bookEntity);
	}
	@Override
	public BookEntity updateBookEntity(BookEntity bookEntity) {
		
		return bookRepository.save(bookEntity);
	}
	@Override
	public void deleteBookEntityById(Long id) {
		bookRepository.delete(id);
		
	}
	@Override
	public List<BookEntity> getCurrentBookList() {
		Sort sort = new Sort(Sort.Direction.DESC,"createTime");
		
		return bookRepository.findAll(sort);
	}
	@Override
	public Page<BookEntity> getPageBookList() {
		Sort sort = new Sort(Sort.Direction.DESC,"createTime");
		Pageable pageable = new PageRequest(0, 5,sort);
		return bookRepository.findAll(pageable);
	}
	@Override
	public List<BookEntity> getBookList() {
		List<BookEntity> bookList = new ArrayList<BookEntity>();
		bookList = bookRepository.findAll();
		return bookList;
	}

}
